package com.inspur.edp.ai.flow.node;

import com.inspur.edp.ai.flow.metadata.AbsNode;
import com.inspur.edp.ai.flow.metadata.Parameter;
import com.inspur.edp.ai.flow.spi.NodeCompiler;
import com.inspur.edp.ai.flow.spi.GetOutputParameter;
import com.inspur.edp.common.expr.ast.ASTContext;
import com.inspur.edp.common.expr.evaluator.ExpressEvaluator;
import com.inspur.edp.common.type.TypeRefer;
import com.inspur.edp.common.type.deserializer.TypeDeserializer;
import com.inspur.edp.common.type.spi.TypeProviderFactory;
import com.inspur.edp.common.type.spi.TypeResourceProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lizhaorui
 * @date 2025/9/26
 * @description
 */
public class StartNodeCompiler implements NodeCompiler, GetOutputParameter {

    @Override
    public String getKind() {
        return StartNode.KIND;
    }

    @Override
    public Map<String, ExpressEvaluator> compile(ASTContext context, AbsNode absNode) {
        StartNode startNode = (StartNode) absNode;
        if (startNode.getInputParams() != null) {
            List<TypeDeserializer> deserializers = new ArrayList<>();
            startNode.setInputDeserializers(deserializers);
            for (Parameter parameter : startNode.getInputParams()) {
                if (parameter.isCustomType()) {
                    // 本身是json类型，无需提供反序列化器
                    deserializers.add(null);
                } else {
                    TypeRefer typeRefer = parameter.getType();
                    TypeResourceProvider provider = TypeProviderFactory.getProvider(typeRefer.getSource());
                    TypeDeserializer deserializer = provider.getDeserializer(typeRefer);
                    deserializers.add(deserializer);
                }
            }
        }
        return null;
    }

    @Override
    public List<Parameter> getOutputParams(ASTContext astContext, AbsNode node) {
        StartNode startNode = (StartNode) node;
        return startNode.getInputParams();
    }
}